﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using FastReflectionLib;
using SZHL.UI.AppCode;
using System.Data;
using SZHL.Data;
using Newtonsoft.Json;

namespace SZHL.UI.API
{
    public class ZXZXManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(ZXZXManage).GetMethod(msg.Action.ToUpper());
            ZXZXManage model = new ZXZXManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }

        /// <summary>
        /// 添加在线咨询
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDZXZX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_ZXZX ZXZX = JsonConvert.DeserializeObject<ZH_ZXZX>(P1);

            if (ZXZX.Title == null)
            {
                msg.ErrorMsg = "标题不能为空";
                return;
            }
            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(ZXZX.Files))
                {
                    ZXZX.Files += "," + fids;
                }
                else
                {
                    ZXZX.Files = fids;
                }
            }

            if (ZXZX.ID == 0)
            {
                //ZXZX.Type = "1";
                //ZXZX.Status = "0";
                //ZXZX.ZXUser = "18737139991";
                var qy = new JH_Auth_QYB().GetEntities().FirstOrDefault();
                if (qy != null)
                {
                    ZXZX.ZXUser = qy.KFUser;
                }
                ZXZX.CRDate = DateTime.Now;
                ZXZX.CRUser = UserInfo.User.UserName;
                ZXZX.CRUserName = UserInfo.User.UserRealName;

                new ZH_ZXZXB().Insert(ZXZX);

                //发送消息
                string content = UserInfo.User.UserRealName + "发起了在线咨询";
                if (!string.IsNullOrEmpty(ZXZX.ZXUser))
                {
                    new JH_Auth_User_CenterB().SendMsg(UserInfo, "zxzx", "在线咨询", content, ZXZX.ID, ZXZX.ZXUser);
                }

            }
            else
            {
                ZXZX.UpdateDate = DateTime.Now;
                ZXZX.UpdateUser = UserInfo.User.UserName;
                ZXZX.UpdateUserName = UserInfo.User.UserRealName;
                new ZH_ZXZXB().Update(ZXZX);
            }
            msg.Result = ZXZX;
        }

        /// <summary>
        /// 添加回复
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDZXZXITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_ZXZX_Item ZXZXITEM = JsonConvert.DeserializeObject<ZH_ZXZX_Item>(P1);

            ZH_ZXZX zxzx = new ZH_ZXZXB().GetEntity(d => d.ID == ZXZXITEM.ZXID);

            if (ZXZXITEM.HFContent == null)
            {
                msg.ErrorMsg = "回复内容不能为空";
                return;
            }

            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(ZXZXITEM.Files))
                {
                    ZXZXITEM.Files += "," + fids;
                }
                else
                {
                    ZXZXITEM.Files = fids;
                }
            }

            if (ZXZXITEM.ID == 0)
            {
                
                //ZXZXITEM.Type = "1";
                //ZXZXITEM.Status = "0";
                
                ZXZXITEM.CRDate = DateTime.Now;
                ZXZXITEM.CRUser = UserInfo.User.UserName;
                ZXZXITEM.CRUserName = UserInfo.User.UserRealName;

                new ZH_ZXZX_ItemB().Insert(ZXZXITEM);
                
                //发送消息
                if (ZXZXITEM.CRUser != zxzx.CRUser)
                {
                    if (zxzx.Status == "0")
                    {
                        zxzx.Status = "1";
                        new ZH_ZXZXB().Update(zxzx);
                    }
                    string content = UserInfo.User.UserRealName + "对你的在线咨询进行了回复";
                    if (!string.IsNullOrEmpty(zxzx.CRUser))
                    {
                        new JH_Auth_User_CenterB().SendMsg(UserInfo, "zxzx", "在线咨询", content, zxzx.ID, zxzx.CRUser);
                    }
                }
                else
                {
                    string content = UserInfo.User.UserRealName + "对你的回复进行了再次咨询";
                    if (!string.IsNullOrEmpty(zxzx.ZXUser))
                    {
                        new JH_Auth_User_CenterB().SendMsg(UserInfo, "zxzx", "在线咨询", content, zxzx.ID, zxzx.ZXUser);
                    }
                }
            }
            else
            {
                ZXZXITEM.UpdateDate = DateTime.Now;
                ZXZXITEM.UpdateUser = UserInfo.User.UserName;
                ZXZXITEM.UpdateUserName = UserInfo.User.UserRealName;
                new ZH_ZXZX_ItemB().Update(ZXZXITEM);
            }
            DataTable dt = new ZH_XTGGB().GetDTByCommand("select * from ZH_ZXZX_Item where id=" + ZXZXITEM.ID.ToString());
            dt.Columns.Add("FileList", Type.GetType("System.Object"));

            string[] fileIds1 = ZXZXITEM.Files.ToString().Split(',');
            dt.Rows[0]["FileList"] = new FT_FileB().GetEntities(p => fileIds1.Contains(p.ID.ToString()));

            msg.Result = dt.Rows[0].Table;
        }

        /// <summary>
        /// 获取在线咨询列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETZXZXIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            bool bl = false;
            var qy = new JH_Auth_QYB().GetEntities().FirstOrDefault();
            if (qy != null)
            {
                if (qy.KFUser.Contains(UserInfo.User.UserName))
                {
                    bl = true;
                }
            }

            string strWhere = string.Empty;

            if (bl)
            {
                strWhere = "1=1";
            }
            else
            {
                strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            }

            if (P1 != "")
            {
                strWhere += string.Format(" and Type='{0}' ", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" and (Title like  '%{0}%' or Description like '%{0}%') ", P2);

            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "0", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_ZXZX", "*", 8, page, "CRDate desc", strWhere, ref total);
            dt.Columns.Add("hfList", Type.GetType("System.Object"));
            dt.Columns.Add("FileList", Type.GetType("System.Object"));
            foreach (DataRow row in dt.Rows)
            {
                DataTable dt1 = new ZH_XTGGB().GetDTByCommand("select * from ZH_ZXZX_Item where zxid=" + row["ID"].ToString());
                dt1.Columns.Add("FileList", Type.GetType("System.Object"));
                foreach (DataRow row1 in dt1.Rows)
                {
                    string[] fileIds1 = row1["Files"].ToString().Split(',');
                    row1["FileList"] = new FT_FileB().GetEntities(p => fileIds1.Contains(p.ID.ToString()));
                }
                row["hfList"] = dt1;

                string[] fileIds = row["Files"].ToString().Split(',');
                row["FileList"] = new FT_FileB().GetEntities(p => fileIds.Contains(p.ID.ToString()));
            }
            msg.Result = dt;
            msg.Result1 = total;

        }
        /// <summary>
        /// 获取在线咨询列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETZXZXIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            bool bl = false;
            var qy = new JH_Auth_QYB().GetEntities().FirstOrDefault();
            if (qy != null)
            {
                if (qy.KFUser.Contains(UserInfo.User.UserName))
                {
                    bl = true;
                }
            }

            string strWhere = string.Empty;

            if (bl)
            {
                strWhere = "1=1";
            }
            else
            {
                strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            }

            if (P1 != "")
            {
                strWhere += string.Format(" and Type='{0}' ", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" and (Title like  '%{0}%' or Description like '%{0}%') ", P2);

            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "0", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_ZXZX", "*", 8, page, "CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;

        }

        /// <summary>
        /// 获取项目列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETZXZXXMIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            bool bl = false;
            var qy = new JH_Auth_QYB().GetEntities().FirstOrDefault();
            if (qy != null)
            {
                if (qy.KFUser.Contains(UserInfo.User.UserName))
                {
                    bl = true;
                }
            }

            string strWhere = string.Empty;

            if (bl)
            {
                strWhere = "1=1";
            }
            else
            {
                strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            }

            //DataTable dt = new ZH_XTGGB().GetDTByCommand("select * from (select Type,CRDate,Row_Number() OVER (partition by Type ORDER BY CRDate desc) rank from dbo.ZH_ZXZX where " + strWhere + ") as XMMC where rank=1");
            DataTable dt = new ZH_XTGGB().GetDTByCommand("select distinct Type from dbo.ZH_ZXZX where " + strWhere);
            msg.Result = dt;
        }

        /// <summary>
        /// 获取项目列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETZXZXXMIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            bool bl = false;
            var qy = new JH_Auth_QYB().GetEntities().FirstOrDefault();
            if (qy != null)
            {
                if (qy.KFUser.Contains(UserInfo.User.UserName))
                {
                    bl = true;
                }
            }

            string strWhere = string.Empty;

            if (bl)
            {
                strWhere = "1=1";
            }
            else
            {
                strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            }

            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "0", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("(select Type,CRDate,Row_Number() OVER (partition by Type ORDER BY CRDate desc) rank from dbo.ZH_ZXZX where " + strWhere + ") as XMMC", " Type,rank", 8, page, "CRDate desc", "rank=1", ref total);
            msg.Result = dt;
            msg.Result1 = total;

        }

        /// <summary>
        /// 删除咨询
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">咨询ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELZXZXBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_ZXZXB().Delete(d => d.ID.ToString() == P1))
                {
                    if (new ZH_ZXZX_ItemB().Delete(d => d.ZXID.ToString() == P1 && d.Type == "1"))
                    {
                        msg.ErrorMsg = "";
                    }
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }

        /// <summary>
        /// 获取咨询信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">咨询Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETZXZXMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_ZXZX gzrz = new ZH_ZXZXB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = gzrz;
            //msg.Result1 = new ZH_ZXZX_ItemB().GetEntities(d => d.ZXID.ToString() == P1);

            DataTable dt1 = new ZH_XTGGB().GetDTByCommand("select * from ZH_ZXZX_Item where zxid=" + P1);
            dt1.Columns.Add("FileList", Type.GetType("System.Object"));
            foreach (DataRow row1 in dt1.Rows)
            {
                string[] fileIds1 = row1["Files"].ToString().Split(',');
                row1["FileList"] = new FT_FileB().GetEntities(p => fileIds1.Contains(p.ID.ToString()));
            }

            msg.Result1 = dt1;

            if (gzrz.Files != null)
            {
                msg.Result2 = new FT_FileB().GetEntities(d => gzrz.Files.Split(',').Contains(d.ID.ToString()));
            }

        }
    }
}